package com.study.io.nio.demo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;

public class DatagramChannelClientTest {

    public static void main(String[] args) {
        try (DatagramChannel channel = DatagramChannel.open();) {
            ByteBuffer buf = ByteBuffer.allocate(100);
            buf.clear();
            buf.put("DatagramChannel Demo".getBytes());
            // 发送数据前注意要把buffer的position置为0
            buf.flip();
            // 调用send方法发送到指定IP地址的指定端口
            channel.send(buf, new InetSocketAddress("localhost", 9999));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
